<template>
  <div class="set">
    <div class="set-top">
      <div class="set-item">
        <div class="desc">
          <span class="word">类别名称</span>
          <span class="tip">*</span>
        </div>
        <div class="right">
          <van-field
            v-model="rec.repairType"
            placeholder="请输入报修类型"
            maxlength="16"
          />
        </div>
      </div>

      <div class="set-item">
        <div class="desc">
          <span class="word">维修人员</span>
          <span class="tip">*</span>
        </div>
        <div
          class="right"
          @click="
            router.replace(
              '/repairlist/set/person?repairType=' +
                rec.repairType +
                '&status=' +
                rec.status +
                '&id=' +
                rec.id
            )
          "
        >
          <span style="color: #303233" v-if="route.query?.type === 'sure'">{{
            store.getters["setOrg/getOrg"]
          }}</span>
          <span v-else>请选择维修人员</span>
          <van-icon name="arrow" />
        </div>
      </div>

      <div class="set-item">
        <div class="desc">
          <span class="word">启用状态</span>
        </div>
        <div class="right switch">
          <van-switch v-model="rec.status" />
        </div>
      </div>
    </div>
    <div class="btn-group">
      <Btn
        btnTitle="保存"
        @submit="toggleShow"
        @cancel="router.replace('/home')"
      />
    </div>
    <van-popup v-model:show="show">
      <div>
        <div class="title">确定要提交？</div>
        <div class="message">
          为确保数据的一致性，请注意：一旦提交表单，将无法再修改类型名称。
        </div>
        <Btn btnTitle="确定" @submit="submitData" @cancel="toggleShow" />
      </div>
    </van-popup>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import router from "@/router";
import Btn from "./cnps/btn/index.vue";
import { addOptions, editOption } from "@/service/modules/option/index";
import { Toast } from "vant";
import { useRoute } from "vue-router";
import { useStore } from "@/store";
document.title = "维修配置";
const store = useStore();
const route = useRoute();
const rec = reactive({
  repairType: route.query?.repairType || "", // 维修类型
  status: route.query?.status === "true", // 启用状态
  personId: store.getters["setOrg/getId"], // 人员
  personName: store.getters["setOrg/getOrg"],
  type: route.query?.type, // 提交类型
  id: route.query?.id,
});
const show = ref(false);
const toggleShow = async () => {
  show.value = !show.value;
};

const submitData = async () => {
  if (!rec.repairType) {
    Toast.fail("请填写报修类型");
    return false;
  }
  if (rec.type === undefined) {
    Toast.fail("请选择维修人员");
    return false;
  }
  if (rec.type === "off") {
    Toast.fail("请选择维修人员");
    return false;
  }
  if (rec.id === "undefined") {
    // 转换id
    rec.id = "";
  }
  let res;
  if (rec.id) {
    // 编辑
    res = await editOption({ ...rec, status: Number(rec.status) });
  } else {
    res = await addOptions({ ...rec, status: Number(rec.status) });
  }
  if (res.code === 0) {
    Toast.success("保存成功");
    setTimeout(() => {
      router.push("/home");
    }, 1500);
  }
};
</script>

<style lang="less" scoped>
.set {
  padding: 0 16px;
  height: 100vh;
  //   display: flex;
  //   flex-direction: column;
  &-top {
    height: calc(100vh - 60px);
  }
  .tip {
    color: red;
  }
  .desc {
    display: flex;
    align-items: center;
  }
  .word {
    font-size: 14px;
    line-height: 49px;
    color: #303233;
  }
  &-item {
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #f5f5f5;
    .right {
      font-size: 14px;
      line-height: 40px;
      color: #c5c5c5;
      span {
        margin-right: 8.75px;
        color: #c5c5c5;
        font-size: 14px;
      }
      /deep/ .van-cell {
        padding: 0;
        input {
          line-height: 40px;
          text-align: right;
          padding-right: 22.75px;
          &::placeholder {
            color: #c5c5c5;
            font-size: 14px;
          }
        }
      }
    }
    .switch {
      display: flex;
      align-items: center;
    }
    /deep/ .van-switch {
      width: 48px;
      height: 24px;
      padding: 0 1px;
      .van-switch__node {
        width: 18px;
        height: 18px;
        border-radius: 18px;
        top: 3px;
      }
    }
    /deep/.van-switch--on {
      background: #3e77fa;
    }
  }
  /deep/ .van-popup {
    width: 295px;
    border-radius: 10px;
    padding: 16px;
    .title {
      font-size: 16px;
      line-height: 22px;
      letter-spacing: 1px;
      font-weight: 500;
      text-align: center;
    }
    .message {
      margin-top: 20px;
      margin-bottom: 18px;
      padding: 0 8px;
      font-size: 14px;
      line-height: 22px;
      color: #919599;
      letter-spacing: 1px;
    }
    .btn {
      button {
        width: 124px;
        border-width: 0.5px;
      }
    }
  }
}
</style>
